route.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // app/api/branches/[branch]/[year]/months/route.js
  2. import { NextResponse } from "next/server";
  3. import { listMonths } from "@/lib/storage";
  4. import { getSession } from "@/lib/auth/session";
  5. import { canAccessBranch } from "@/lib/auth/permissions";
  6. /**
  7. * GET /api/branches/[branch]/[year]/months
  8. */
  9. export async function GET(request, ctx) {
  10. const session = await getSession();
  11. if (!session) {
  12. return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  13. }
  14. const { branch, year } = await ctx.params;
  15. console.log("[/api/branches/[branch]/[year]/months] params:", {
  16. branch,
  17. year,
  18. });
  19. if (!branch || !year) {
  20. return NextResponse.json(
  21. { error: "branch oder year fehlt" },
  22. { status: 400 }
  23. );
  24. }
  25. if (!canAccessBranch(session, branch)) {
  26. return NextResponse.json({ error: "Forbidden" }, { status: 403 });
  27. }
  28. try {
  29. const months = await listMonths(branch, year);
  30. return NextResponse.json({ branch, year, months });
  31. } catch (error) {
  32. console.error("[/api/branches/[branch]/[year]/months] Error:", error);
  33. return NextResponse.json(
  34. { error: "Fehler beim Lesen der Monate: " + error.message },
  35. { status: 500 }
  36. );
  37. }
  38. }